package com.seeyon.apps.bense.bank.sync;

import cn.hutool.core.lang.Assert;
import com.seeyon.apps.bense.bank.core.Constants;
import com.seeyon.apps.bense.bank.manager.TravelPaymentManagerImpl;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.quartz.QuartzHolder;
import com.seeyon.ctp.common.quartz.QuartzJob;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时任务-差旅费报销
 */
public class TravelPaymentBackTask implements QuartzJob {

    private final static Log logger = LogFactory.getLog(TravelPaymentBackTask.class);

    /**
     * 间隔同步的小时和分钟数（默认为:10分钟）
     */
    private static int intervalHour = 23;

    private static int intervalMin = 10;

    private static int triggerHour = 23;

    private static int triggerMinute = 10;

    /**
     * 定义的任务插件ID
     */
    private static String jobBeanId = "travelPaymentBackTask";

    /**
     * 定义的任务名称
     */
    private static String jobName = "QuartzSyncTravelPaymentBack";

    private TravelPaymentManagerImpl impl = (TravelPaymentManagerImpl) AppContext.getBean("travelPaymentManagerImpl");

    @Override
    public void execute(Map<String, String> map) {
        logger.info("***差旅费报销结果同步事件开始**");
        try {
            impl.doSync();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("差旅费报销结果同步任务发生异常", e);
            e.printStackTrace();
        }

        logger.info("***差旅费报销结果同步事件结束**");
    }

    /**
     * 注册同步任务（V8.0）
     */
    public static void registerTask(boolean canExcute) {
        logger.info("差旅费报销结果同步调度任务" + TravelPaymentBackTask.class.getSimpleName() + "...");
        try {
            if (!canExcute) {
                QuartzHolder.deleteQuartzJob(jobName);
                return;
            }
            // 先删除任务
            QuartzHolder.deleteQuartzJob(jobName);
            // 设定时间
            Map<String, String> parameters = new HashMap<String, String>();
            String cron = Constants.travelPaymentCron;
            Assert.notBlank(cron, "差旅费报销结果定时cron表达式不能为空");
            QuartzHolder.newCronQuartzJob(null, jobName, cron, new Date(), null, jobBeanId, parameters);
            logger.info("差旅费报销结果同步任务" + TravelPaymentBackTask.class.getSimpleName() + ",成功");
        } catch (Exception e) {
            logger.error("差旅费报销结果同步调度任务发生异常", e);
        }
    }
}
